﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class vote : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["in_vote"] == null)
            Session["in_vote"] = "";
        try
        {
            Loading();
        }
        catch { return; }
       
    }

    private void Loading()
    {
        DataTable dt = new DataTable();
        dt = new ltModel.BLL.Class().GetList("1"," kg=0 order by [time] desc ").Tables[0];
        if (dt.Rows.Count > 0)
        {
            lbltitle.Text = dt.Rows[0]["title"].ToString();
            lblClassid.Text = dt.Rows[0]["id"].ToString();
            lblstime.Text = DateTime.Parse(dt.Rows[0]["time"].ToString()).ToShortDateString();
            lbletime.Text = dt.Rows[0]["overtime"].ToString();
            //if (lbletime.Text.IndexOf(":") <= 0) lbletime.Text = lbletime.Text + " 00:00:00";
            DataTable dtv = new DataTable();
            dtv = new ltModel.BLL.vote().GetList("[class]=" + dt.Rows[0]["id"].ToString() + " order by [id] asc ").Tables[0];
            if (dt.Rows[0]["style"].ToString() == "checkbox")
            {
                CheckBoxList1.Visible = true;
                for (int i = 0; i < dtv.Rows.Count; i++)
                {
                    CheckBoxList1.Items.Add(dtv.Rows[i]["vote"].ToString());
                    CheckBoxList1.Items[i].Value = dtv.Rows[i]["id"].ToString();
                }
            }
            else
            {
                RadioButtonList1.Visible = true;
                for (int i = 0; i < dtv.Rows.Count; i++)
                {
                    RadioButtonList1.Items.Add(dtv.Rows[i]["vote"].ToString());
                    RadioButtonList1.Items[i].Value = dtv.Rows[i]["id"].ToString();
                }
            }
        }
        dt.Dispose();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string val = "";

        try
        {

            if (Convert.ToDateTime(lbletime.Text) < DateTime.Now)
            {
                Response.Write("<script>alert('该调查已结束！');window.open('voteshow.aspx?id=" + lblClassid.Text + "');window.location='" + Request.Url.ToString() + "';</script>");
                return;
            }

            if ((Session["in_vote"].ToString() + "|").IndexOf((lblClassid.Text + "|")) > 0)
            {
                Response.Write("<script>alert('您已经投过票了！');window.open('voteshow.aspx?id=" + lblClassid.Text + "');window.location='" + Request.Url.ToString() + "';</script>");
                return;
            }

        }
        catch (Exception ex)
        { Response.Write("<script>alert('"+ex.ToString()+"！');window.location='" + Request.Url.ToString() + "';</script>"); return; }

        if (RadioButtonList1.Visible == true)
        {
            if (RadioButtonList1.SelectedValue != "")
                val = RadioButtonList1.SelectedValue;
        }
        else
        {
            if (CheckBoxList1.SelectedValue != "")
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                    if (CheckBoxList1.Items[i].Selected == true)
                        val = val + CheckBoxList1.Items[i].Value + ",";
        }

        if (val != "")
        {
            if (val.IndexOf(",") > 0)
                val = val.Substring(0, val.Length - 1);
        }
        else
        {
            Response.Write("<script>alert('请选择投票项！');window.location='" + Request.Url.ToString() + "';</script>");
            return;
        }

        if (new ltModel.BLL.vote().VoteAdd(val))
        {
            Session["in_vote"] = Session["in_vote"].ToString() + "|" + lblClassid.Text;

            Response.Write("<script>alert('投票成功！');window.open('voteshow.aspx?id=" + lblClassid.Text + "');window.location='" + Request.Url.ToString() + "';</script>");
        }
        else
            Response.Write("<script>alert('投票失败！');window.location='" + Request.Url.ToString() + "';</script>");
    }
}
